//
//  HMTabBar.swift
//  sinaweibo
//
//  Created by WGP on 16/4/8.
//  Copyright © 2016年 WGP. All rights reserved.
//

import UIKit

class HMTabBar: UITabBar {
    
    typealias tabBarItemClosure = (()->Void)
    var addClosuer:tabBarItemClosure?
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        let averWidth:CGFloat  = frame.width/CGFloat(HMMainTabBarButtonCount)
        
        var index = 0
        for item in subviews
        {
            if item.isKindOfClass(NSClassFromString("UITabBarButton")!)
            {
                item.frame = CGRect(x: CGFloat(index) * averWidth, y: 0, width: averWidth, height: frame.height)
                 index += index==1 ? 2 : 1
            }
           
        }
        
        addBtn.frame = CGRect(x: averWidth*2, y: 0, width: averWidth, height: frame.height)
    }
    
    @objc private func didAddBtnClick()
    {
        addClosuer?()
    }

    
//    MARK: - 懒加载加号按钮
    private lazy var addBtn:UIButton =
    {
        let btn = UIButton()
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: .Highlighted)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: .Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: .Highlighted)
        
        btn.addTarget(self, action: Selector("didAddBtnClick"), forControlEvents: .TouchUpInside)
        
//        添加到TabBar上,闭包里一定要加self
        self.addSubview(btn)
        
        return btn
    }()

}
